package com.example.chiro;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

class GetGPS extends AsyncTask<Void, Integer, Location> implements
		LocationListener {

	final long REFRESH = 5 * 1000;
	private Location lastLocation;
	private LocationManager lm;
	long depart = 0, fin = 0;
	String best;

	protected void onPreExecute() {
		depart = System.currentTimeMillis();
		// Log.i("T: onPreExcute", "debut");
		Criteria crit = new Criteria();
		crit.setAccuracy(Criteria.ACCURACY_FINE);

		// Configure location manager - I'm using just the network provider in
		// this example
		lm = (LocationManager) Arbre.context.getSystemService(Context.LOCATION_SERVICE);
		
		
		best = lm.getBestProvider(crit, false);
		Log.i("T: onPreExecute", "best : " + best);

		Log.i("lm", lm.toString());
		
		//lm.requestLocationUpdates(best, 1, 1, this);
		//lastLocation = lm.getLastKnownLocation(best);
		
		lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1, 1, this);
		Log.i("provider", LocationManager.GPS_PROVIDER);
		lastLocation = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
		
		if (lastLocation.getAltitude()>0)
			Slide_general.altitude.setText(String.valueOf(lastLocation.getAltitude()));
		else 
			Slide_general.altitude.setText("NC");
		Slide_general.latitude.setText(String.valueOf(lastLocation.getLatitude()));
		Slide_general.longitude.setText(String.valueOf(lastLocation.getLongitude()));
		Slide_general.maj.setText(last_time_maj(lastLocation));
		
	}

	protected Location doInBackground(Void... params) {
		do {
			lastLocation = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} while (System.currentTimeMillis() - lastLocation.getTime() > REFRESH);
		return lastLocation;
	}

	protected void onPostExecute(Location location_r) {
		
		if (lastLocation.getAltitude()!=0.0)
			Slide_general.altitude.setText(String.valueOf(lastLocation.getAltitude()));
		else 
			Slide_general.altitude.setText("NC");
		Slide_general.latitude.setText(String.valueOf(lastLocation.getLatitude()));
		Slide_general.longitude.setText(String.valueOf(lastLocation.getLongitude()));
		Slide_general.maj.setText(last_time_maj(lastLocation));
		
		fin = System.currentTimeMillis();
		Slide_general.rafraichir.setVisibility(View.VISIBLE);
		Slide_general.rafraichir_chargement.setVisibility(View.GONE);
		
		lm = (LocationManager) Arbre.context
				.getSystemService(Context.LOCATION_SERVICE);
		lm.removeUpdates(this);
		
		return;
	}

	@Override
	public void onLocationChanged(Location arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onProviderDisabled(String arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onCancelled(){
		lm = (LocationManager) Arbre.context
				.getSystemService(Context.LOCATION_SERVICE);
		lm.removeUpdates(this);
		//Toast.makeText(Arbre.context, "Cancel !", Toast.LENGTH_SHORT).show();
	}
	
	private String last_time_maj(Location l){
		Date tmp = new Date(lastLocation.getTime());
		SimpleDateFormat format_H = new SimpleDateFormat("HH");
		SimpleDateFormat format_M = new SimpleDateFormat("mm");
		SimpleDateFormat format_S = new SimpleDateFormat("ss");
		format_H.setTimeZone(TimeZone.getDefault());
		format_M.setTimeZone(TimeZone.getDefault());
		format_S.setTimeZone(TimeZone.getDefault());
		return format_H.format(tmp)+"h"+format_M.format(tmp)+" et "+format_S.format(tmp)+"s";
	}
	
	
}

